package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.entity.Study;
import cn.edu.nit.sys.service.StudyService;
import cn.edu.nit.sys.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 罗文斌
 * @Date 2023/5/6
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/study")
public class StudyController {

    @Autowired
    private StudyService studyService;

    @PostMapping
    public R addStudyRoom(@RequestBody Study study) {
        return studyService.saveOrUpdate(study) ? R.ok("操作成功") : R.error("操作失败");
    }

    @GetMapping
    public R getStudyList(Page<Study> page, String siftName) {
        return studyService.getStudyList(page, siftName);
    }

    @PutMapping("/{id}")
    public R updateStatus(@PathVariable("id") String id, String status) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        if("0".equals(status)) {
            status = "1";
        }else if("1".equals(status)) {
            status = "0";
        }
        updateWrapper.set("status", status);
        updateWrapper.eq("id", id);
        return studyService.update(updateWrapper) ? R.ok("操作成功") : R.error("操作失败");
    }

    @DeleteMapping("/{id}")
    public R delStudyRoom(@PathVariable("id") String id) {
        return studyService.removeById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @GetMapping("/detail/{id}")
    public R getDetail(@PathVariable("id") String id) {
        return studyService.getDetail(id);
    }

    @PostMapping("/log")
    public R addStudyLog(@RequestBody Map<String, String> params) {
        return studyService.addStudyLog(params);
    }

    @GetMapping("/log")
    public R studyLog(Page<Map<String, String>> page, String uid) {
        return studyService.getLogList(page, uid);
    }
}
